import unittest
from time import sleep

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from atstudy_Login.login import login


class goStudy(login):
    def test_goStudy(self):
        self.br.implicitly_wait(5)
        # 点击学习中心
        self.br.find_element(By.CLASS_NAME,'study-center').click()
        sleep(2)
        # 点击继续学习
        self.br.find_element(By.XPATH,'//*[@id="__layout"]/div/div[2]/div[2]/div[2]/div[3]/ul/li[1]/div[3]/button').click()
        sleep(2)
        # 找到返回图标
        e0= WebDriverWait(self.br,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//*[@id="__layout"]/main/section/div[1]/div/div[1]/span[2]')))
        e0 .click()
        sleep(2)
        # 每次滚动 500 像素，直到页面底部
        for i in range(10):
            self.br.execute_script("window.scrollBy(0, 500);")
            sleep(1)  # 等待一秒，防止滚动太快
            # 找到第22章:web端自动化测试-selenium（前置课：web开发、python、unittest）元素停止
            try:
                e=self.br.find_element(By.XPATH,'//*[@id="__layout"]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div[23]/section/div/button')
                break
            except Exception:
                print('元素未找到，继续滚动')
        # 点击【22-1：selenium-概念和环境
        self.br.find_element(By.XPATH,'//*[@id="__layout"]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div[23]/section/div[2]/section[1]/div/div/div/div/div/span').click()
        # 悬停【直播】 selenium-概念和环境
        e=self.br.find_element(By.XPATH,'//*[@id="__layout"]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div[23]/section/div[2]/section[1]/div/div/ul/li[1]/p/span[1]')
        ActionChains(self.br).move_to_element(e).perform()
        sleep(2)
        self.br.find_element(By.XPATH,'//*[@id="__layout"]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div[23]/section/div[2]/section[1]/div/div/ul/li[1]/div/button[3]').click()
        sleep(2)
        # 定位页面标题
        e1=self.br.find_element(By.XPATH,'//*[@id="__layout"]/main/section/div[1]/div/div[2]')
        self.assertTrue(e1,'没有到播放页面')
# #         退出
#         self.test_logOut()

if __name__ == '__main__':
    ts=unittest.TestSuite()
    ts.addTest(goStudy('test_goStudy'))
    tr=unittest.TextTestRunner
    tr.run(ts)

